package com.xxx.service.impl;

import com.xxx.dao.SurveyDao;
import com.xxx.entity.MPage;
import com.xxx.entity.Survey;
import com.xxx.entity.dto.SurveyDto;
import com.xxx.entity.query.SurveyQuery;
import com.xxx.service.ISurveyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xxx
 * @since 2024-11-24
 */
@Service
@Transactional
public class SurveyServiceImpl extends ServiceImpl<SurveyDao, Survey> implements ISurveyService {

    @Autowired
    private SurveyDao surveyDao;

    @Override
    public MPage selectByCondition(SurveyQuery surveyQuery) {
        int current = surveyQuery.getCurrent();
        int size = surveyQuery.getSize();
        surveyQuery.setCurrent(null);
        List<SurveyDto> list = surveyDao.selectByCondition(surveyQuery);
        int total = list.size();
        surveyQuery.setCurrent((current-1)*size);
        list = surveyDao.selectByCondition(surveyQuery);
        MPage page = MPage.builder()
                .current(current)
                .size(size)
                .total(total)
                .pages(total/size+total%size>0?1:0)
                .records(list)
                .build();
        return page;
    }

    @Override
    public MPage selectByPage(Integer current, Integer size) {
        int total = surveyDao.selectCount(null);
        List<SurveyDto> list = surveyDao.selectByPage((current-1)*size,size);
        MPage page = MPage.builder()
                .current(current)
                .size(size)
                .total(total)
                .pages(total/size+total%size>0?1:0)
                .records(list)
                .build();
        return page;
    }

    @Override
    public Map<String, Object> incomeAnalysis() {
        Map<String, Object> map = surveyDao.incomeAnalysis();
        return map;
    }
}
